package com.ghost.view.pop
{
	import com.ghost.data_struct.HashMap;
	import com.ghost.manager.Effect_manager;
	import com.ghost.manager.Pop_win_manager;
	import com.ghost.model.Effect_data;
	import com.ghost.model.Global_data;
	import com.ghost.model.Object_pool;
	import com.ghost.utils.GC;
	import com.ghost.utils.MovieClipUtil;
	import com.ghost.view.Base_effect;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;

	/**
	 * @author ghost
	 * 存放所有弹出窗口的容器
	 */
	public class Pop_main extends Base_effect
	{
		private var bg_shape:Sprite;
		public var bg_bool:Boolean;//背景是否可见
		private var pop_hashmap:HashMap;//存放显示状态的pop
		private var show_pop_width:Number=0;//所有显示pop的宽度
		public function Pop_main()
		{
			pop_hashmap = new HashMap();
			bg_shape = new Sprite();
			bg_shape.graphics.beginFill(0xfffff0,0.5);
			bg_shape.graphics.drawRect(0,0,Global_data.stage_s_width,Global_data.stage_s_height);
			bg_shape.graphics.endFill();
			this.addChild(bg_shape);
			bg_shape.visible = false;
			Pop_win_manager.getInstance().pop_main = this;
		}
		/**
		 *创建pop 
		 * @param mc 要创建的pop对象
		 * @param count_width_bool是否需要计算pop宽度(loading就不需要)，默认为true
		 */		
		public function Creat_pop_win(mc:*,count_width_bool:Boolean=true):void
		{
			this.addChild(mc);
			mc.x = this.width/2-mc.width/2;
			mc.y = this.height/2-mc.height/2;
			Effect_manager.getInstance().set_effect(mc,Effect_data.fade_in_obj);
			if(!Object_pool.pop_win_pool.containsKey(mc.name))Object_pool.pop_win_pool.put(mc.name,mc);
			if(mc.type == 0)
			{
				if(count_width_bool)show_pop_width +=mc.width;
				pop_hashmap.put(mc.name,mc);
				reflash_x_y();
				bg_bool = false;
			}
			else
			{
				bg_shape.visible = true;
				bg_bool = true;
				this.setChildIndex(bg_shape,this.numChildren-2);
			}
		}
		/**
		 * 移除pop
		 * @param mc 要移除的对象
		 * @param count_width_bool是否需要计算pop宽度(loading就不需要)，默认为true
		 */		
		public function Remove_pop_win(mc:*,count_width_bool:Boolean=true):void
		{
			if(this.contains(mc))
			{
				this.removeChild(mc);
				//mc = null;
				GC.nowGC();
				if(mc.type == 0)
				{
					pop_hashmap.delete_key_content(mc.name);
					if(count_width_bool)show_pop_width -= mc.width;
					reflash_x_y();
				}
				else bg_shape.visible = false;
				bg_bool = false;
			}
		}
		/**
		 *改变容器大小并且改变所有显示pop的位置 
		 */		
		public function reflash_x_y():void
		{
			bg_shape.width = Global_data.stage_s_width;
			bg_shape.height = Global_data.stage_s_height;
			var l:int = pop_hashmap.size();
			var i:int;
			var clone_width:Number = show_pop_width;
			if(show_pop_width<=Global_data.stage_s_width)
			{
				for(;i<l;i++)
				{
					pop_hashmap.values()[i].x = (Global_data.stage_s_width-show_pop_width)/2+(pop_hashmap.values()[i].width)*i;
					pop_hashmap.values()[i].y = Global_data.stage_s_height/2-pop_hashmap.values()[i].height/2;
				}
			}
			else
			{
				var s:int=1;
				while(clone_width>=Global_data.stage_s_width)
				{
					clone_width -=pop_hashmap.values()[l-s].width;
					s++;
				}
				for(;i<l;i++)
				{
					if(i>l-s)
					{
						pop_hashmap.values()[i].x = Global_data.stage_s_width/2-pop_hashmap.values()[i].width/2;
					}
					else
					{
						pop_hashmap.values()[i].x = (Global_data.stage_s_width-clone_width)/2+(pop_hashmap.values()[i].width)*i;
					}
					pop_hashmap.values()[i].y = Global_data.stage_s_height/2-pop_hashmap.values()[i].height/2;
				}
			}
		}
		
	}
}